package com.hspedu.newData.dmsxl.动态规划.基础;

import org.junit.Test;

import java.util.Arrays;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-06 
 * @Version: 1.0
 */

public class LC322零钱兑换 {

    // 动态规划
    public int coinChange(int[] coins, int amount) {
        int[] dp =  new int[amount + 1];
        int max = amount + 1;
        Arrays.fill(dp,max);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < coins.length; j++) {
                if (coins[j] <= i) {
                    dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                }
            }
        }
        return dp[amount] = dp[amount] == amount + 1 ? -1: dp[amount];
    }

    @Test
    public void test() {
        // 测试代码
        int i = coinChange(new int[]{1, 2, 5}, 11);
        int i1 = coinChange(new int[]{2}, 3);
        int i2 = coinChange(new int[]{1}, 0);
        System.out.println("=");
    }
}
